<template>
  <div class="app-container">
    <el-row>
      <el-form :inline="true" :model="pageParam" label-width="80px" label-position="left">
        <el-form-item label="页面名称">
          <el-input v-model="pageParam.name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">查询</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button type="primary" @click="handleAddPage">新增页面</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="margin-top:30px"
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="页面名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="插件名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status | statusFilter"
          >{{ scope.row.status == '1' ? '启用' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-pagination
        layout="prev, pager, next"
        :current-page.sync="pageParam.current"
        :page-size.sync="pageParam.size"
        :total="pageParam.total"
        @current-change="handleCurrentChange"
      />
    </el-row>

    <edit-page
      v-if="dialogVisible"
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :page-info="pageInfo"
      @closeToggle="closeDialog"
    />
  </div>
</template>

<script>
import { getSyncPageList } from "@/api/page";
import EditPage from "./EditPage";

export default {
  name: "PageManager",
  components: {
    EditPage
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        "1": "success",
        "0": "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogVisible: false,
      dialogType: "New",
      pageInfo: null,
      pageParam: {
        "code": "",
        "current": 1,
        "name": "",
        "size": 10,
        "total": 0
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getSyncPageList(this.pageParam).then(response => {
        const data = response.data;
        this.list = data.records;
        this.pageParam.total = data.total;
        this.pageParam.size = data.size;
        this.pageParam.current = data.current;
        this.listLoading = false;
      });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageParam.current = val;
      this.fetchData();
    },
    handleAddPage() {
      this.dialogType = "New";
      this.dialogVisible = true;
    },
    handleEdit(idx, info) {
      this.pageInfo = Object.assign({}, info);
      this.dialogType = "Edit";
      this.dialogVisible = true;
    },
    closeDialog(needRefresh) {
      this.dialogVisible = false;
      if (needRefresh) {
        this.fetchData();
      }
    }
  }
};
</script>
